#ifndef __COMPONENT_MINER_H__
#define __COMPONENT_MINER_H__

#include "Component.hpp"
#include <cstdint>
#include "Game/IInventory.hpp"
#include "Game/CellFace.h"
#include <memory>
#include "GameEnum.h"
#include "Subsystems/SubsystemGameInfo.h"
#include "Managers/BlocksManager.h"
#include "Terrain/Terrain.h"
#include "Game/RaycastData.h"


namespace PocketSurvival
{
    struct ComponentBody;
    struct ComponentPlayer;
    struct ComponentCreature;

    // struct ComponentCreature;
    struct ComponentMiner : public Component
    {
        float digStartTime;
        float digProgress;
        int64_t lastHitTime;
        float lastPokingPhase;
        IInventory *inventory = nullptr;

        float attackPower;
        float pokingPhase;
        std::shared_ptr<CellFace> digCellFace = nullptr;
        bool digFaceChange = false;

        ComponentPlayer *componentPlayer = nullptr;
        ComponentCreature *componentCreature = nullptr;


        inline int32_t getActiveBlockValue();
        inline uint32_t getActiveBlockCount();
        inline float getDigTime();
        inline float getDigProgress();
        // inline void poke(bool forceRestart);

        bool dig(const TerrainRaycastResult &raycastResult, bool isEnd = false);
        bool place(const TerrainRaycastResult &raycastResult);
        bool place(const TerrainRaycastResult &raycastResult, int32_t value);
        bool use(const Ray3& ray);
        bool interact(const TerrainRaycastResult &raycastResult);
        bool aim(const Ray3 &aim, AimState state);

        RaycastData raycast(const Ray3 &ray, RaycastMode mode);

        void removeActiveTool(int32_t removeCount);
        void damageActiveTool(int32_t damageCount);
        float calculateDigTime(int32_t digValue, int32_t toolContents);
        bool canUseTool(int32_t toolValue);

        virtual void update(float dt) override;
		virtual const std::string &getName() const override;
        static const std::string &GetName();
        virtual void load(const nlohmann::json &jsonData) override;
        virtual void save(nlohmann::json &jsonData) override;
        virtual void saveToMsgJson(nlohmann::json &jsonData) override;
    };

    inline int32_t ComponentMiner::getActiveBlockValue()
    {
		if (inventory == nullptr)
		{
			return 0;
		}
        if(inventory->getSlotCount(inventory->activeSlotIndex) == 0)
        {
            return 0;
        }
		return inventory->getSlotValue(inventory->activeSlotIndex);
    }
    inline uint32_t ComponentMiner::getActiveBlockCount()
    {
		if (inventory == nullptr)
		{
			return 0;
		}
        return inventory->getSlotCount(inventory->activeSlotIndex);
    }

    inline float ComponentMiner::getDigTime()
    {
        return 0;
    }
    inline float ComponentMiner::getDigProgress()
    {
        return digProgress;
    }
    
} // namespace PocketSurvival

#endif
